<%@ page import="com.Web.Person.Student" %>
<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2024/8/6
  Time: 14:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>updateStudent</title>
    <link href="static/layui/css/layui.css" rel="stylesheet">
</head>
<body>
    <script src="static/layui/layui.js" type="text/javascript" charset="UTF-8"></script>
    <script src="static/jquery-2.1.4.js" type="text/javascript" charset="UTF-8"></script>
    <script src="static/mylayer.js" type="text/javascript" charset="UTF-8"></script>

    <form class="layui-form layui-form-pane" lay-filter="formFilter" action="">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" autocomplete="off" placeholder="请输入姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="age" autocomplete="off" placeholder="请输入年龄" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="text" name="gender" autocomplete="off" placeholder="请输入性别" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">班级</label>
            <div class="layui-input-block">
                <select id="banjiId" name="banjiId">
                    <option value="">请选择班级</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submitDemo">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script>

        $(function () {
            $.post(
                '/banji?method=selectAll',
                {'banjiId':$(this).val()},
                function (jsonObj) {
                    console.log(jsonObj);
                    $(jsonObj).each(function () {
                        $('#banjiId').append('<option value="' + this.id + '">' + this.name + '</option>');
                    })
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render();
                    });
                },
                'json'
            )
        })

        layui.use(['form'], function () {
            var form = layui.form;

            var queryString = window.location.search;
            var urlParams = new URLSearchParams(queryString);
            var id = urlParams.get("id");

            console.log("id: " + id);

            form.render();

            $.post(
                "/student?method=selectById",
                {"id": id},
                function (result) {
                    console.log(result);
                    if(result.code == 0) {
                        form.val("formFilter", result.data);
                    }
                },
                'json'
            );

            //监听提交
            form.on('submit(submitDemo)', function(data){
                // layer.alert(JSON.stringify(data.field), {
                //     title: '最终的提交信息'
                // })
                console.log(data.field);
                $.post(
                    '/student?method=update',
                    data.field,
                    function (result) {
                        console.log(result);
                        if(result.code == 0) {
                            mylayer.okMsg(result.msg);
                            setInterval(function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload();
                            }, 2000);
                        }
                    },
                    'json'
                )
                return false;
            });

        })
    </script>
</body>
</html>
